package com.sxt.sys.controller;

import com.sxt.sys.constant.SYSConstant;
import com.sxt.sys.service.OperatorService;
import com.sxt.sys.utils.DataView;
import com.sxt.sys.utils.MD5Utils;
import com.sxt.sys.utils.ResultObj;
import com.sxt.sys.vo.OperatorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 管理和操作员控制器
 * @author WanMing
 * @date 2019/2/3 14:41
 */

@Controller
@RequestMapping("operator")
public class OperatorController {


    @Autowired
    private OperatorService operatorService;

    /**
     * 跳转到操作员列表页面
     * @return
     */
    @RequestMapping("toLoadAllOperator")
    public String toLoadAllOperator(){
        return "system/operatorManager";
    }

    /**
     * 加载操作员页面数据
     * @param operatorVo
     * @return
     */
    @RequestMapping("loadAllOperator")
    @ResponseBody
    public DataView loadAllOperator(OperatorVo operatorVo){
        DataView dataView = operatorService.queryAllOperator(operatorVo);
        return dataView;
    }

    /**
     * 打开添加操作员页面
     * @return
     */
    @RequestMapping("toAddOperator")
    public String toAddOperator(){
        return "system/addOperator";
    }

    /**
     * 添加操作员
     * @param operatorVo
     * @return
     */
    @RequestMapping("addOperator")
    public ResultObj addOperator(OperatorVo operatorVo){
        //密码加密处理
        operatorVo.setOperatorPwd(MD5Utils.getMD5(operatorVo.getOperatorPwd(),operatorVo.getOperatorName(),SYSConstant.ENCIPHERMENT_NUMBER));
        ResultObj resultObj = null;
        try {
            operatorService.addOperator(operatorVo);
            resultObj = new ResultObj(SYSConstant.SUCCESS_CODE,SYSConstant.ADD_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            resultObj = new ResultObj(SYSConstant.ERROR_CODE,SYSConstant.ADD_ERROR);
        }

        return resultObj;
    }
}
